package algorithm.dynamicProgramming.leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
//139. 单词拆分
public class WordBreak {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("leet");
        list.add("code");
        System.out.println(wordBreak("leetcode", list));
    }
    public static boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        boolean[] f = new boolean[s.length()+1];
        f[0]=true;
        for (int i=1;i<=s.length();i++){
            for (int j =0;j<i;j++){
                if (f[j] && set.contains(s.substring(j,i))){
                    f[i] =true;
                    break;
                }
            }
        }
        return f[s.length()];
    }


}
